package com.sc.sysconfig.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sc.sysconfig.dto.DictQueryDTO;
import com.sc.sysconfig.entity.SysDict;
import com.sc.sysconfig.service.SysDictService;
import com.sc.sysconfig.vo.OptionVO;
import com.sc.sysconfig.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * sys dict控制器
 *
 * @author 徐赫阳
 * @date 2021/09/13
 */
@Api(description = "字典")
@RestController
@RequestMapping("/dict")
public class SysDictController {

    /**
     * 服务对象
     */
    @Autowired
    private SysDictService sysDictService;

    @ApiOperation("分页获取字典")
    @PostMapping("/getDictList")
    public PageVO<SysDict> getDictList(@RequestBody DictQueryDTO queryDTO){
        return sysDictService.getDictList(queryDTO);
    }

    @ApiOperation("根据字典名获取字典")
    @GetMapping("/getDict/{dict}")
    public List<OptionVO> getDict(@PathVariable String dict){
        return sysDictService.getDict(dict);
    }

    @ApiOperation("根据字典名获取字典")
    @PostMapping("/getDictName/{dict}/{val}")
    public String getDict(@PathVariable String dict, @PathVariable Integer val){
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDict::getName,dict).isNull(SysDict::getParent);
        List<SysDict> list = sysDictService.list(queryWrapper);
        if (list != null && !list.isEmpty()){
            SysDict sysDict1 = list.get(0);
            QueryWrapper<SysDict> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.lambda().eq(SysDict::getParent,sysDict1.getId()).eq(SysDict::getDictValue,val);
            List<SysDict> list2 = sysDictService.list(queryWrapper2);
            if(list2 != null && !list2.isEmpty()){
                return list2.get(0).getName();
            }else{
                return "";
            }
        }else{
            return "";
        }
    }

    @ApiOperation("保存字典")
    @PostMapping("/saveDict")
    public Integer saveDict(@RequestBody SysDict sysDict, HttpServletRequest request) throws Exception{
        if(sysDict.getParent() != null && sysDict.getId() != null && sysDict.getParent() == sysDict.getId()){
            throw new Exception("不可以嵌套选择哦！~~");
        }
        if(sysDict.getId() == null){
            sysDict.setCreatorId((Integer)request.getSession(true).getAttribute("user"));
        }else{
            sysDict.setUpdaterId((Integer)request.getSession(true).getAttribute("user"));
        }
        sysDictService.saveOrUpdate(sysDict);
        return sysDict.getId();
    }

    @ApiOperation("获取字典父级下拉列表")
    @GetMapping(value = "/getDictOption")
    public List<OptionVO> getDictOption(){
        return sysDictService.getDictOption();
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/deleteDict/{id}")
    public void deleteDict(@PathVariable Integer id){
        sysDictService.removeById(id);
    }




}
